﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ComUIControl
{
    /// <summary>
    /// 使用指南信息绘制板块
    /// </summary>
    public partial class GuideBoard : UserControl
    {
        /// <summary>
        /// 指南信息文本
        /// </summary>
        private List<string> listGuideInfo = new List<string>();

        /// <summary>
        /// 指南信息文本竖直方向上的位置
        /// </summary>
        private List<float> listGuideInfoYPos = new List<float>();

        /// <summary>
        /// 默认字体
        /// </summary>
        private Font fontDefault = new Font("微软雅黑", 32);

        ///// <summary>
        ///// 高亮字体
        ///// </summary>
        //private Font fontLighted = new Font("微软雅黑", 36);

        private int maxHeightIndex;
        private int maxWidthIndex;

        public GuideBoard()
        {
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();
            this.BackColor = Color.FromArgb(45, 222, 195, 111);
        }

        public void InitGuideInfo(List<string> list)
        {
            listGuideInfo = list;
            ProcessShowText();
        }

        private void ProcessShowText()
        {
            maxHeightIndex = 0;
            maxWidthIndex = 0;
            int maxLength = 0;
            for (int i = listGuideInfo.Count - 1; i >= 0; i--)
            {
                string strTemp = listGuideInfo[i];
                if (strTemp.Trim() == string.Empty)
                {
                    listGuideInfo.RemoveAt(i);
                    continue;
                }
                string[] sArray = strTemp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (sArray.Count() > maxHeightIndex)
                {
                    maxHeightIndex = i;
                }
                strTemp = string.Format("{0}. {1}", i + 1, sArray[0]);

                if (sArray[0].Length > maxLength)
                {
                    maxLength = sArray[0].Length;
                    maxWidthIndex = i;
                }
                for (int k = 1; k < sArray.Count(); k++)
                {
                    if (sArray[k].Length > maxLength)
                    {
                        maxLength = sArray[k].Length;
                        maxWidthIndex = i;
                    }
                    strTemp = string.Format("{0}\n    {1}", strTemp, sArray[k]);
                }
                listGuideInfo[i] = strTemp;
            }
        }

        private int _nDistance = 70;

        public Size MeasureTextSize()
        {
            float width = 0;
            float lastBottom = _nDistance;
            listGuideInfoYPos.Clear();
            SizeF stringSize = new SizeF();
            using (Graphics g = this.CreateGraphics())
            {
                for (int i = 0; i < listGuideInfo.Count; i++)
                {
                    if (i > 0)
                    {
                        lastBottom += (stringSize.Height + _nDistance);
                    }
                    listGuideInfoYPos.Add(lastBottom);
                    //if (i == maxWidthIndex || i == maxHeightIndex)
                    //{
                    //    stringSize = g.MeasureString(listGuideInfo[i], fontLighted);
                    //}
                    //else
                    //{
                    //    stringSize = g.MeasureString(listGuideInfo[i], fontDefault);
                    //}
                    stringSize = g.MeasureString(listGuideInfo[i], fontDefault);
                    if (i == 0)
                    {
                        width = stringSize.Width;
                    }
                    else
                    {
                        if (stringSize.Width > width)
                        {
                            width = stringSize.Width;
                        }
                    }
                }
            }
            lastBottom += (stringSize.Height + _nDistance);
            Size size = new Size((int)width, (int)lastBottom);
            return size;
        }

        private void MeasureTextYPos()
        {
            float lastBottom = _nDistance;
            listGuideInfoYPos.Clear();
            using (Graphics g = this.CreateGraphics())
            {
                SizeF stringSize = new SizeF();
                for (int i = 0; i < listGuideInfo.Count; i++)
                {
                    if (i > 0)
                    {
                        lastBottom += (stringSize.Height + _nDistance);
                    }
                    listGuideInfoYPos.Add(lastBottom);
                    stringSize = g.MeasureString(listGuideInfo[i], fontDefault);
                    //if (i == highlightedIndex)
                    //{
                    //    stringSize = g.MeasureString(listGuideInfo[i], fontLighted);
                    //}
                    //else
                    //{
                    //    stringSize = g.MeasureString(listGuideInfo[i], fontDefault);
                    //}
                }
            }

        }

        /// <summary>
        /// 绘制指南信息
        /// </summary>
        /// <param name="highlightedIndex">需要高亮显示的索引</param>
        public void DrawGuideInfo()
        {
            MeasureTextYPos();
            float xPos = 30.0f;
            using (Graphics g = this.CreateGraphics())
            {
                g.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
                for (int i = 0; i < listGuideInfo.Count; i++)
                {
                    g.DrawString(listGuideInfo[i], fontDefault, Brushes.DarkOrange, new PointF(xPos, listGuideInfoYPos[i]));
                    //if(i == highlightedIndex)
                    //{
                    //    g.DrawString(listGuideInfo[i], fontLighted, Brushes.Orange, new PointF(xPos, listGuideInfoYPos[i]));
                    //}
                    //else
                    //{
                    //    g.DrawString(listGuideInfo[i], fontDefault, Brushes.Gray, new PointF(xPos, listGuideInfoYPos[i]));
                    //}                   
                }
            }
        }

    }
}
